﻿using Prism.Commands;
using Prism.Events;
using SoonCode.FocusTest.Events;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Prism.Mvvm;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.ViewModels;
using SoonCode.FocusTest.Views;

namespace SoonCode.FocusTest.ViewModels
{
    internal class AnswerItemGViewModel:BindableBase
    {
        private string testType = "itemG";
        private int TestItemIndex = 7;
        private ObservableCollection<FItemHelper> answerItems;
        private IEventAggregator aggregator;
        private EventInfo answerInfo;
        private string levelImage = "";
        private bool start=false;

        public bool Start
        {
            get { return start; }
            set
            {
                start=value;
                RaisePropertyChanged();
            }
        }
        public string LevelImage
        {
            get { return levelImage; }
            set
            {
                levelImage= value;
                RaisePropertyChanged();
            }
        }
       
 

        public AnswerItemGViewModel(IEventAggregator aggregator)
        {
            this.aggregator = aggregator;
            this.answerInfo = new EventInfo();
            answerInfo.Type = testType;
            AnswerCommand = new DelegateCommand<int?>(AnswerCommandHandler);
            SubmitAnwserCommand = new DelegateCommand(SubmitAnwserCommandHandler);
            this.aggregator.GetEvent<ChangeTestEvent>().Subscribe(p =>
            {
                LevelImage = "";
                Start = false;
                this.aggregator.GetEvent<CloseStudentFullScreenEvent>().Publish();
            });
            this.aggregator.GetEvent<LevelUpdateEvent>().Subscribe(info =>
            {
                if (info.Type != testType) return;
                Start = true;
                LevelImage = (string)info.Data["img"];
            });
            this.aggregator.GetEvent<QuestionChangeEvent>().Subscribe(info =>
            {
                if (info.Type != testType) return;
            });
            this.aggregator.GetEvent<TestLoadEvent>().Subscribe(OnLoaded);
            this.aggregator.GetEvent<StudentStartTestEvent>().Subscribe((eventInfo) =>
            {
                if(eventInfo.Type!= testType) return;
                this.aggregator.GetEvent<OpenStudentFullScreenEvent>().Publish(nameof(AnswerItemGFull));
            });
            
        }

        private void OnLoaded(int p)
        {
            if (p != TestItemIndex-1) return;
            LevelImage = "";
            Start = false;
        }

        public ICommand AnswerCommand { get; set; }
        public ICommand SubmitAnwserCommand { get; set; }

        private void SubmitAnwserCommandHandler()
        {
            this.aggregator.GetEvent<StudentAnswerEvent>().Publish(answerInfo);
        }
        private void AnswerCommandHandler(int? p)
        {
            this.aggregator.GetEvent<StudentAnswerEvent>().Publish(answerInfo);
        }

    }
}
